(function () {
    'use strict';

    angular.module('KingAdmin.pages.sys.dict')
        .controller('DictListCtrl', DictListCtrl);

    /** @ngInject */
    function DictListCtrl($scope, $filter, $uibModal, toastr, DictService) {
        var kt = this;
        kt.dictlist = [];

        
        kt.save = function (dict) {
            if (dict.code == null || dict.code == '') {
                toastr.warning('编号不能为空', "提示:", {"progressBar": true,});
                return;
            }
            DictService.save(dict,function (data) {
                kt.LoadPage();
            });
        }

        kt.LoadPage = function (tableState) {
            tableState = tableState || kt.tableState;
            tableState.pagination.number = tableState.pagination.number || 5;
            DictService.getSmartData(tableState,
                function (data) {
                    tableState.pagination.numberOfPages = data.result.pages;
                    tableState.pagination.totalItemCount = data.result.total;
                    kt.tableState = tableState;
                    kt.dictlist = data.result.records;
                });
        };

        kt.addDict = function (item) {
            console.log(item);
            var modalInstance = $uibModal.open({
                templateUrl: 'app/pages/sys/dict/dict.html',
                controller: 'DictCtrl',
                backdrop: 'static',
                size: 'md',
                resolve: {
                    item: function () {
                        console.log('addDev');
                        return item;
                    }
                }
            });

             modalInstance.closed.then(function(){//模态窗口打开之后执行的函数   
                  console.log('modal is closed');   
                  kt.LoadPage();
              });    
        }

        //删除
        kt.del = function (id) {
            if(id==null){
                kt.LoadPage();
                return;
            }
            DictService.del({id: id},
                function (data) {
                    kt.LoadPage();
                })
        };
        kt.checkboxes = {
            checked: false,
            items: {}
        };
        $scope.$watch('kt.checkboxes.checked', function (value) {
            angular.forEach(kt.dictlist, function (item) {
                kt.checkboxes.items[item.id] = value;
            });
        });
    }

})();
